package org.skycastle.megastructure


import com.jme.input.action._
import com.jme.input.{KeyBindingManager, KeyInput, InputHandler}
import com.jme.math.Vector3f
import com.jme.scene.Spatial

/**
 *
 *
 * @author Hans Haggstrom
 */

class CharacterInputHandler(character: Character) extends InputHandler {
  private val tempVec = new Vector3f()

  setKeyBindings()
  setActions()

  /**
   * creates the keyboard object, allowing us to obtain the values of a keyboard as keys are
   * pressed. It then sets the actions to be triggered based on if certain keys are pressed (WSAD).
   * @param api
   */
  private def setKeyBindings() {
    val keyboard = KeyBindingManager.getKeyBindingManager();

    keyboard.set("forward", KeyInput.KEY_W);
    keyboard.set("backward", KeyInput.KEY_S);
    keyboard.set("turnRight", KeyInput.KEY_D);
    keyboard.set("turnLeft", KeyInput.KEY_A);
    keyboard.set("fire", KeyInput.KEY_SPACE);
  }

  /**
   * Initialize actions available to player
   */
  private def setActions() {

    def addActionHelper(name: String, repeatable: Boolean, action: (Float) => Unit) {

      val actionCommand = new InputActionInterface {
        def performAction(evt: InputActionEvent) {
          action(evt.getTime)
        }
      }

      addAction(actionCommand, name, repeatable)
    }

    addActionHelper("forward", true, character.moveForward)
    addActionHelper("backward", true, character.moveBack)
    addActionHelper("turnRight", true, character.turnRight)
    addActionHelper("turnLeft", true, character.turnLeft)
    addActionHelper("fire", false, character.fire)
  }


}